#include "stdafx.h"
#include "NtScene.h"
#include "NtApplication.h"
#include "NtTransactionProcess.h"
#include "NtVisibleObject.h"
#include "NtTP_MainProcess.h"
#include "NtD3DLight9.h"

NtScene::NtScene(void)
: m_Device9(NULL)
{
}

NtScene::~NtScene(void)
{
}

void NtScene::Update( DWORD dwTimer )
{

}

void NtScene::Render()
{
	m_DefaultCamera.ApplyCamera();

	m_Device9->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x020202AA, 1.0f, 0);
	m_Device9->BeginScene();

	for(size_t i=0; i<m_VisibleObjectList.size(); ++i)
	{
		m_VisibleObjectList[i]->Render();
	}

	m_Device9->EndScene();
	m_Device9->Present(NULL, NULL, NULL, NULL);
}


void NtScene::InitScene()
{
	m_Device9 = NtApplication::GetInstance()->GetDevice();

	NtTP_MainProcessPtr pMainProcess= new NtTP_MainProcess();
	pMainProcess->Init();
	AddTransactionProcess(pMainProcess);

	HWND hWnd= NtApplication::GetInstance()->GetHwnd();
	RECT rc;
	GetClientRect(hWnd, &rc);
	
	float f = (float)(rc.right -rc.left) / (float)(rc.bottom- rc.top);

	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, f, 1.0f, 1000.0f);
	m_Device9->SetTransform(D3DTS_PROJECTION, &proj);

	D3DXMATRIX WorldMatrix;
	memset(&WorldMatrix, 0, sizeof(D3DXMATRIX));
	WorldMatrix.m[0][0] = 1.0f;
	WorldMatrix.m[1][1] = 1.0f;
	WorldMatrix.m[2][2] = 1.0f;
	WorldMatrix.m[3][3] = 1.0f;
	m_Device9->SetTransform(D3DTS_WORLD, &WorldMatrix);

	D3DXCOLOR color(1.0f, 1.0f, 1.0f, 1.0f);
	D3DXVECTOR3 Pos(20.0f, 100.0f, 20.0f);
	NtD3DLight9 Light;
	Light.SetPointLight(&Pos, &color);

	m_Device9->SetLight(0, &Light);

	m_Device9->LightEnable(0, TRUE);

	m_Device9->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
	m_Device9->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
	m_Device9->SetRenderState(D3DRS_ZENABLE, TRUE);
	m_Device9->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
	// m_Device9->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
	//m_Device9->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	//m_Device9->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_Device9->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA);
	m_Device9->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

	/// m_Device9->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);

	m_Device9->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	m_Device9->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	m_Device9->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}

LRESULT NtScene::OnMessage( HWND hWnd, UINT Id, WPARAM wParam, LPARAM lParam )
{
	switch( Id )
	{
	case WM_KEYDOWN:
	case WM_KEYUP:
	case WM_CHAR:
	case WM_MOUSEMOVE:
	case WM_LBUTTONDOWN:
	case WM_LBUTTONUP:
	case WM_RBUTTONDOWN:
	case WM_RBUTTONUP:
	case WM_MBUTTONDOWN:
	case WM_MBUTTONUP:
	case WM_LBUTTONDBLCLK:
	case WM_RBUTTONDBLCLK:
	case WM_MOUSEWHEEL:
		{
			m_DefaultCamera.OnMouseMessage(Id, wParam, lParam);

			for(size_t i=0; i< m_TransactionProcessVector.size(); ++i)
			{
				if ( m_TransactionProcessVector[i]->OnMessage(hWnd, Id, wParam, lParam))
				{
					break;
				}
			}
		}
		break;
	}

	return 0;
}

void NtScene::ReleaseScene()
{
	m_TransactionProcessVector.clear();
	m_VisibleObjectList.clear();
}
